class UserController < ApplicationController

  before_filter :login_required, :only => :my_account
  def login
    @user = User.new
    @user.username = params[:username]
  end

  def process_login
    if user = User.authenticate(params[:username],params[:password])
      session[:id] = user.id # Remember the user's id during this session
      redirect_to :controller=> 'user',:action => 'my_account'
       
    else
      flash[:error] = 'Invalid login.'
      redirect_to :action => 'login'
    end
  end

  def logout
    reset_session
    flash[:message] = 'Logged out.'
    redirect_to :action => 'login'
  end

  def my_account

  end

  def fb_chk
    @facebook_id=params[:fb_id]
    session[:fb_id] = @facebook_id
    @user=User.find_by_fb_id(@facebook_id)

    if @user.blank?      
      redirect_to :controller=> 'user',:action => 'first_run'

    else
      if @user1 = User.authenticate(@user.username,@user.password)
        session[:id] = @user1.id
        redirect_to :controller=> 'user',:action => 'my_account'
      else 
        flash[:error] = 'Invalid login.'
        redirect_to :action => 'login'
      end
    end
  end

  def update

    if @user = User.authenticate(params[:username],params[:password])  
      @user.update_attribute("fb_id",session[:fb_id])
      session[:id] = @user.id 
      redirect_to :controller=> 'user',:action => 'my_account'

    else
      flash[:error] = 'Invalid login.'
      redirect_to :action => 'first_run'
    end

  end

  def sign_up
    if request.post? and params[:user]
      @user = User.new(params[:user])

      if @user.save
        flash[:error] = "User created."
        redirect_to :action => "login"
      end
    end
  end
 end
